﻿/*****************************************
 * 
 *  copyright (c) by ylshen
 * 
 *  author : ylshen (syl.lucky@gmail.com)
 * 
 **************************************** */

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using JYStockAnalyser.Face;
using JYStockAnalyser.Model;
using JYStockAnalyser.ClientLib;

namespace JYStockAnalyser.ClientLib
{
    /// <summary>
    /// 分页器
    /// </summary>
    public class Pager : UserControl
    {
        private Button btnNext;
        private Label lblPageInfo;
        private Button btnPre;

        public Pager()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.btnNext = new System.Windows.Forms.Button();
            this.btnPre = new System.Windows.Forms.Button();
            this.lblPageInfo = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // btnNext
            // 
            this.btnNext.Location = new System.Drawing.Point(139, 0);
            this.btnNext.Name = "btnNext";
            this.btnNext.Size = new System.Drawing.Size(75, 23);
            this.btnNext.TabIndex = 0;
            this.btnNext.Text = "下一页";
            this.btnNext.UseVisualStyleBackColor = true;
            this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
            // 
            // btnPre
            // 
            this.btnPre.Location = new System.Drawing.Point(0, 0);
            this.btnPre.Name = "btnPre";
            this.btnPre.Size = new System.Drawing.Size(75, 23);
            this.btnPre.TabIndex = 1;
            this.btnPre.Text = "上一页";
            this.btnPre.UseVisualStyleBackColor = true;
            this.btnPre.Click += new System.EventHandler(this.btnPre_Click);
            // 
            // lblPageInfo
            // 
            this.lblPageInfo.AutoSize = true;
            this.lblPageInfo.Font = new System.Drawing.Font("SimSun", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.lblPageInfo.ForeColor = System.Drawing.Color.White;
            this.lblPageInfo.Location = new System.Drawing.Point(84, 5);
            this.lblPageInfo.Name = "lblPageInfo";
            this.lblPageInfo.Size = new System.Drawing.Size(49, 14);
            this.lblPageInfo.TabIndex = 2;
            this.lblPageInfo.Text = "label1";
            // 
            // Pager
            // 
            this.BackColor = System.Drawing.Color.Black;
            this.Controls.Add(this.lblPageInfo);
            this.Controls.Add(this.btnPre);
            this.Controls.Add(this.btnNext);
            this.Name = "Pager";
            this.Size = new System.Drawing.Size(215, 24);
            this.Load += new System.EventHandler(this.Pager_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void Pager_Load(object sender, EventArgs e)
        {        
            m_init();
        }

        public void SetPageInfo(int pageIndex, int count)
        {
            m_currentPageIndex = pageIndex;
            m_pageCount = count;
            m_refreshPageInfo();
        }

        int m_currentPageIndex = 0;
        int m_pageCount = 0;

        void m_init()
        {
            m_refreshPageInfo();
        }

        private void m_refreshPageInfo()
        {
            this.lblPageInfo.Text = String.Format("{0}/{1}", m_currentPageIndex, m_pageCount);
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (m_currentPageIndex != m_pageCount)
            {
                m_currentPageIndex += 1;
                OnPageIndexChanged(new PageIndexChangedEventArgs(m_currentPageIndex));
                m_refreshPageInfo();
            }
        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            if (m_currentPageIndex != 1)
            {
                m_currentPageIndex -= 1;
                OnPageIndexChanged(new PageIndexChangedEventArgs(m_currentPageIndex));
                m_refreshPageInfo();
            }
        }

        public delegate void PageIndexChangedHandler(PageIndexChangedEventArgs e);

        public event PageIndexChangedHandler PageIndexChanged;

        protected void OnPageIndexChanged(PageIndexChangedEventArgs e)
        {
            if (PageIndexChanged != null)
            {
                PageIndexChanged(e);
            }
        }
    }

    public class PageIndexChangedEventArgs : EventArgs
    {
        public int NewPageIndex { get; set; }

        public PageIndexChangedEventArgs(int pageIndex)
        {
            NewPageIndex = pageIndex;            
        }
    }
}
